#include <iostream>
#include <vector>
#include <string>

int main() {
    int flag = 0, n;
    std::cin >> n;
    std::string s[n], t;
    std::vector<std::string> ss;
    for(int i = 0; i < n; i++) {
        std::cin >> s[i];
    }
    std::cin >> t;
    for(int i = 0; i < n; i++) {
        if(s[i] == t) {
            ss.push_back(s[i]);
            flag = 1;
            break;
        }
        if(s[i] == "return") ss.pop_back();
        else ss.push_back(s[i]);
    }
    if(flag) {
        for(int i = 0; i < ss.size(); i++) {
            if(i) std::cout << "->";
            std::cout << ss[i];
        }
        std::cout << std::endl;
    } else std::cout << "NOT REFERENCED" << std::endl;
    return 0;
}